<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/blog/images/logo.svg" color="#222">
  <meta name="baidu-site-verification" content="code-ia4sE56m4l">

<link rel="stylesheet" href="/blog/css/main.css">


<link rel="stylesheet" href="/blog/lib/font-awesome/css/all.min.css">
  
  <link rel="stylesheet" href="/blog/lib/animate-css/animate.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"tangmonk-meat.gitee.io","root":"/blog/","scheme":"Muse","version":"8.0.0-rc.4","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":true,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":true,"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":"valine","storage":true,"lazyload":true,"nav":null,"activeClass":"valine"},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"path":"search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}};
  </script>

  <meta name="description" content="1 多级反馈队列调度算法1.1 作用如果有很多任务排队等着被处理，哪个任务先被处理，哪个任务后处理，这个需要由操作系统决定，这就是调度。 多级反馈队列调度算法 是目前操作系统调度算法中被公认的一种较好的调度算法。它可以满足各种类型进程的需要，既能使高优先级的作业得到响应又能使短作业（进程）迅速完成。">
<meta property="og:type" content="article">
<meta property="og:title" content="多级反馈队列调度算法">
<meta property="og:url" content="https://tangmonk-meat.gitee.io/blog/2021/03/19/%E5%A4%9A%E7%BA%A7%E5%8F%8D%E9%A6%88%E9%98%9F%E5%88%97%E8%B0%83%E5%BA%A6%E7%AE%97%E6%B3%95/index.html">
<meta property="og:site_name" content="唐僧肉BLOG">
<meta property="og:description" content="1 多级反馈队列调度算法1.1 作用如果有很多任务排队等着被处理，哪个任务先被处理，哪个任务后处理，这个需要由操作系统决定，这就是调度。 多级反馈队列调度算法 是目前操作系统调度算法中被公认的一种较好的调度算法。它可以满足各种类型进程的需要，既能使高优先级的作业得到响应又能使短作业（进程）迅速完成。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://s1.ax1x.com/2020/10/22/BF5A3j.png">
<meta property="article:published_time" content="2021-03-19T13:16:54.990Z">
<meta property="article:modified_time" content="2020-10-23T06:21:46.318Z">
<meta property="article:author" content="TANGMONK-MEAT">
<meta property="article:tag" content="进程">
<meta property="article:tag" content="C++">
<meta property="article:tag" content="算法">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://s1.ax1x.com/2020/10/22/BF5A3j.png">

<link rel="canonical" href="https://tangmonk-meat.gitee.io/blog/2021/03/19/%E5%A4%9A%E7%BA%A7%E5%8F%8D%E9%A6%88%E9%98%9F%E5%88%97%E8%B0%83%E5%BA%A6%E7%AE%97%E6%B3%95/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>多级反馈队列调度算法 | 唐僧肉BLOG</title>
  






  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <main class="main">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader">
        <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/blog/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">唐僧肉BLOG</h1>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">记录生活中的点点滴滴</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/blog/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/blog/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/blog/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">44</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/blog/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">17</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/blog/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">38</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <section class="post-toc-wrap sidebar-panel">
          <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#1-%E5%A4%9A%E7%BA%A7%E5%8F%8D%E9%A6%88%E9%98%9F%E5%88%97%E8%B0%83%E5%BA%A6%E7%AE%97%E6%B3%95"><span class="nav-number">1.</span> <span class="nav-text">1 多级反馈队列调度算法</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-1-%E4%BD%9C%E7%94%A8"><span class="nav-number">1.1.</span> <span class="nav-text">1.1 作用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-2-%E5%BA%94%E7%94%A8%E8%8C%83%E5%9B%B4"><span class="nav-number">1.2.</span> <span class="nav-text">1.2 应用范围</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-3-%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5"><span class="nav-number">1.3.</span> <span class="nav-text">1.3 基本概念</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-4-%E4%BD%BF%E7%94%A8%E6%96%B9%E6%B3%95%E5%8F%8A%E6%AD%A5%E9%AA%A4"><span class="nav-number">1.4.</span> <span class="nav-text">1.4 使用方法及步骤</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-5-%E6%BA%90%E7%A0%81"><span class="nav-number">1.5.</span> <span class="nav-text">1.5 源码</span></a></li></ol></li></ol></div>
      </section>
      <!--/noindex-->

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="TANGMONK-MEAT"
      src="https://s1.ax1x.com/2020/09/22/wOscW9.jpg">
  <p class="site-author-name" itemprop="name">TANGMONK-MEAT</p>
  <div class="site-description" itemprop="description">直到这一刻微笑着说话为止，我至少留下了一公升眼泪</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/blog/archives/">
        
          <span class="site-state-item-count">38</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/blog/categories/">
          
        <span class="site-state-item-count">17</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/blog/tags/">
          
        <span class="site-state-item-count">44</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/TANGMONK-MEAT" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;TANGMONK-MEAT" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:2950371251@qq.com" title="E-Mail → mailto:2950371251@qq.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://gitee.com/TANGMONK-MEAT" title="Gitee → https:&#x2F;&#x2F;gitee.com&#x2F;TANGMONK-MEAT" rel="noopener" target="_blank"><i class="iconfont icongitee-fill-round fa-fw"></i>Gitee</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://www.leancloud.cn/" title="LeanCloud → https:&#x2F;&#x2F;www.leancloud.cn" rel="noopener" target="_blank"><i class="iconfont iconCloudupload fa-fw"></i>LeanCloud</a>
      </span>
  </div>
  <div class="cc-license animated" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/zh-CN" class="cc-opacity" rel="noopener" target="_blank"><img src="/blog/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>



      </section>
    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </header>

      
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

  <a href="https://github.com/TANGMONK-MEAT" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>

<noscript>
  <div id="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


      <div class="main-inner">
        

        <div class="content post posts-expand">
          

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://tangmonk-meat.gitee.io/blog/2021/03/19/%E5%A4%9A%E7%BA%A7%E5%8F%8D%E9%A6%88%E9%98%9F%E5%88%97%E8%B0%83%E5%BA%A6%E7%AE%97%E6%B3%95/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="https://s1.ax1x.com/2020/09/22/wOscW9.jpg">
      <meta itemprop="name" content="TANGMONK-MEAT">
      <meta itemprop="description" content="直到这一刻微笑着说话为止，我至少留下了一公升眼泪">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="唐僧肉BLOG">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          多级反馈队列调度算法
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2021-03-19 21:16:54" itemprop="dateCreated datePublished" datetime="2021-03-19T21:16:54+08:00">2021-03-19</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-10-23 14:21:46" itemprop="dateModified" datetime="2020-10-23T14:21:46+08:00">2020-10-23</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/blog/categories/%E6%93%8D%E4%BD%9C%E7%B3%BB%E7%BB%9F/" itemprop="url" rel="index"><span itemprop="name">操作系统</span></a>
                </span>
            </span>

          
            <span id="/blog/2021/03/19/%E5%A4%9A%E7%BA%A7%E5%8F%8D%E9%A6%88%E9%98%9F%E5%88%97%E8%B0%83%E5%BA%A6%E7%AE%97%E6%B3%95/" class="post-meta-item leancloud_visitors" data-flag-title="多级反馈队列调度算法" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span>
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/blog/2021/03/19/%E5%A4%9A%E7%BA%A7%E5%8F%8D%E9%A6%88%E9%98%9F%E5%88%97%E8%B0%83%E5%BA%A6%E7%AE%97%E6%B3%95/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/blog/2021/03/19/%E5%A4%9A%E7%BA%A7%E5%8F%8D%E9%A6%88%E9%98%9F%E5%88%97%E8%B0%83%E5%BA%A6%E7%AE%97%E6%B3%95/" itemprop="commentCount"></span>
    </a>
  </span>
  
  <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>7.5k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>7 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h1 id="1-多级反馈队列调度算法"><a href="#1-多级反馈队列调度算法" class="headerlink" title="1 多级反馈队列调度算法"></a>1 多级反馈队列调度算法</h1><h2 id="1-1-作用"><a href="#1-1-作用" class="headerlink" title="1.1 作用"></a>1.1 作用</h2><p>如果有很多任务排队等着被处理，哪个任务先被处理，哪个任务后处理，这个需要由操作系统决定，这就是调度。</p>
<p><strong><em>多级反馈队列调度算法</em></strong> 是目前操作系统调度算法中被公认的一种较好的调度算法。它可以满足各种类型进程的需要，既能使高优先级的作业得到响应又能使短作业（进程）迅速完成。</p>
<a id="more"></a>
<h2 id="1-2-应用范围"><a href="#1-2-应用范围" class="headerlink" title="1.2 应用范围"></a>1.2 应用范围</h2><p>此算法应用于同一个资源的多个使用者可分优先级使用资源的情况</p>
<h2 id="1-3-基本概念"><a href="#1-3-基本概念" class="headerlink" title="1.3 基本概念"></a>1.3 基本概念</h2><p>多级反馈队列调度算法是一种根据先来先服务原则给就绪队列排序，为就绪队列赋予不同的优先级数，不同的时间片，按照优先级抢占CPU的调度算法。算法的实施过程如下：</p>
<ol>
<li><p>按照先来先服务原则排序，设置N个就绪队列为Q1，Q2…QN，每个队列中都可以放很多作业；</p>
</li>
<li><p>为这N个就绪队列赋予不同的优先级，第一个队列的优先级最高，第二个队列次之，其余各队列的优先权逐个降低；</p>
</li>
<li><p>设置每个就绪队列的时间片，优先权越高，算法赋予队列的时间片越小。时间片大小的设定按照实际作业（进程）的需要调整；</p>
</li>
<li><p>进程在进入待调度的队列等待时，首先进入优先级最高的Q1等待。</p>
</li>
<li><p>首先调度优先级高的队列中的进程。若高优先级中队列中已没有调度的进程，则调度次优先级队列中的进程。例如：Q1,Q2,Q3三个队列，只有在Q1中没有进程等待时才去调度Q2，同理，只有Q1,Q2都为空时才会去调度Q3。</p>
</li>
<li><p><strong><em>对于同一个队列中的各个进程，按照 ( 时间片轮转法调度 )</em></strong>。比如Q1队列的时间片为N，那么Q1中的作业在经历了时间片为N的时间后，若还没有完成，则进入Q2队列等待，若Q2的时间片用完后作业还不能完成，一直进入下一级队列，直至完成。</p>
</li>
<li><p>在低优先级的队列中的进程在运行时，又有新到达的作业，那么在运行完这个时间片后，CPU马上分配给新到达的作业即抢占式调度CPU。</p>
</li>
</ol>
<h2 id="1-4-使用方法及步骤"><a href="#1-4-使用方法及步骤" class="headerlink" title="1.4 使用方法及步骤"></a>1.4 使用方法及步骤</h2><blockquote>
<p>假设系统中有3个就绪队列Q1,Q2,Q3，时间片分别为2，4，8。</p>
<p>现在有3个作业J1,J2,J3分别在时间 0 ，1，3时刻到达。</p>
<p>而它们所需要的CPU时间分别是3，2，1个时间片。</p>
</blockquote>
<ol>
<li><p>时刻0： J1到达。于是进入到队列1 ， 运行1个时间片 ， 时间片还未到，此时J2到达。</p>
</li>
<li><p>时刻1： J2到达。 由于时间片仍然由J1掌控，于是等待。 J1在运行了1个时间片后，已经完成了在Q1中的2个时间片的限制，于是J1置于Q2等待被调度。现在处理机分配给J2。</p>
</li>
<li><p>时刻2： J1进入Q2等待调度，J2获得CPU开始运行。</p>
</li>
<li><p>时刻3：J3到达，由于J2的时间片未到，故J3在Q1等待调度，J1也在Q2等待调度。</p>
</li>
<li><p>时刻4：J2处理完成，由于J3，J1都在等待调度，但是J3所在的队列比J1所在的队列的优先级要高，于是J3被调度，J1继续在Q2等待。</p>
</li>
<li><p>时刻5：J3经过1个时间片，完成。</p>
</li>
<li><p>时刻6：由于Q1已经空闲，于是开始调度Q2中的作业，则J1得到处理器开始运行。</p>
</li>
<li><p>时刻7：J1再经过一个时间片，完成了任务。于是整个调度过程结束。</p>
</li>
</ol>
<p><a target="_blank" rel="noopener" href="https://imgchr.com/i/BF5A3j"><img data-src="https://s1.ax1x.com/2020/10/22/BF5A3j.png" alt="BF5A3j.png"></a></p>
<h2 id="1-5-源码"><a href="#1-5-源码" class="headerlink" title="1.5 源码"></a>1.5 源码</h2><blockquote>
<p>编写并调试一个模拟的进程调度程序，采用“多级反馈队列轮转法”调度算法对五个进程进行调度。</p>
<p>注意：linux 平台下的 unistd.h 头文件相当于 windows平台的 windows.h 头文件</p>
</blockquote>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;queue&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;list&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;unistd.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;math.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> <span class="built_in">std</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">unsigned</span> <span class="keyword">int</span> q_id = <span class="number">0</span>;                <span class="comment">// 队列进程号的全局变量</span></span><br><span class="line"><span class="keyword">unsigned</span> <span class="keyword">int</span> stime = <span class="number">0</span>;               <span class="comment">// 进程开始执行的时间</span></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">unsigned</span> <span class="keyword">int</span> Q_NUM = <span class="number">3</span>;         <span class="comment">// 总就绪队列数</span></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">unsigned</span> <span class="keyword">int</span> TOTAL_PRO_NUM = <span class="number">5</span>; <span class="comment">// 总进程数</span></span><br><span class="line"><span class="keyword">const</span> <span class="keyword">unsigned</span> <span class="keyword">int</span> INIT_PRO_NUM = <span class="number">3</span>;  <span class="comment">// 初始就绪进程数</span></span><br><span class="line"><span class="keyword">unsigned</span> <span class="keyword">int</span> pro_num = <span class="number">3</span>;             <span class="comment">// 当前进程个数</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 进程信息</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Pro</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="keyword">unsigned</span> <span class="keyword">int</span> pid;        <span class="comment">// 进程ID</span></span><br><span class="line">    <span class="keyword">unsigned</span> <span class="keyword">int</span> start_time; <span class="comment">// 开始执行时间</span></span><br><span class="line">    <span class="keyword">unsigned</span> <span class="keyword">int</span> need_time;  <span class="comment">// 预计执行时间</span></span><br><span class="line">    <span class="keyword">unsigned</span> <span class="keyword">int</span> end_time;   <span class="comment">// 完成时间</span></span><br><span class="line">    <span class="keyword">unsigned</span> <span class="keyword">int</span> run_time;   <span class="comment">// 已经运行时间</span></span><br><span class="line">    <span class="keyword">unsigned</span> <span class="keyword">int</span> count;      <span class="comment">// 计数器</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 多级就绪队列节点信息</span></span><br><span class="line"><span class="class"><span class="keyword">struct</span> <span class="title">Node</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line">    <span class="built_in">queue</span>&lt;Pro&gt; q;          <span class="comment">// 就绪队列</span></span><br><span class="line">    <span class="keyword">unsigned</span> <span class="keyword">int</span> priority; <span class="comment">// 队列优先级</span></span><br><span class="line">    <span class="keyword">unsigned</span> <span class="keyword">int</span> cap;      <span class="comment">// 时间片</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 进程调度类</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Scheduling</span></span></span><br><span class="line"><span class="class">&#123;</span></span><br><span class="line"><span class="keyword">private</span>:</span><br><span class="line">    <span class="built_in">queue</span>&lt;Pro&gt; qp;    <span class="comment">// 队列</span></span><br><span class="line">    <span class="built_in">list</span>&lt;Pro&gt; lp;     <span class="comment">// 链表</span></span><br><span class="line">    <span class="built_in">list</span>&lt;Node&gt; ln;    <span class="comment">// 链表队列</span></span><br><span class="line">    <span class="keyword">unsigned</span> <span class="keyword">int</span> cap; <span class="comment">// 时间片</span></span><br><span class="line"><span class="keyword">public</span>:</span><br><span class="line">    Scheduling(<span class="comment">/* args */</span>);</span><br><span class="line">    ~Scheduling();</span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">create_q_pro</span><span class="params">()</span></span>; <span class="comment">// 创建进程queue的函数</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">create_l_pro</span><span class="params">()</span></span>; <span class="comment">// 创建进程list的函数</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">create_node</span><span class="params">()</span></span>;  <span class="comment">// 创建node队列</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">fcfs</span><span class="params">()</span></span>;         <span class="comment">// FCFS ： 先来先服务调度算法</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">rr</span><span class="params">()</span></span>;           <span class="comment">// RR : 轮转调度算法</span></span><br><span class="line">    <span class="function"><span class="keyword">void</span> <span class="title">mfq</span><span class="params">()</span></span>;          <span class="comment">// MFQ : 多级反馈队列调度算法</span></span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">Scheduling::Scheduling(<span class="comment">/* args */</span>)</span><br><span class="line">&#123;</span><br><span class="line">    cap = <span class="number">10</span>; <span class="comment">// 初始化时间片</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">Scheduling::~Scheduling()</span><br><span class="line">&#123;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Scheduling::create_q_pro</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Pro item;</span><br><span class="line">    item.pid = (++q_id);</span><br><span class="line">    item.count = <span class="number">0</span>;</span><br><span class="line">    item.need_time = rand() % <span class="number">100</span> + <span class="number">10</span>; <span class="comment">// 预计需求时间 在 10 ～ 100 之间</span></span><br><span class="line">    item.run_time = <span class="number">0</span>;</span><br><span class="line">    <span class="comment">//</span></span><br><span class="line">    qp.push(item);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;创建进程 PID= %d:  执行所需时间= %d\n&quot;</span>, item.pid, item.need_time);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Scheduling::create_l_pro</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Pro item;</span><br><span class="line">    item.pid = ++q_id;</span><br><span class="line">    item.count = <span class="number">0</span>;</span><br><span class="line">    item.need_time = rand() % <span class="number">200</span> + <span class="number">10</span>; <span class="comment">// 预计需求时间 在 10 ～ 100 之间</span></span><br><span class="line">    item.run_time = <span class="number">0</span>;</span><br><span class="line">    <span class="comment">//</span></span><br><span class="line">    lp.push_back(item);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;创建进程 PID = %d:   执行所需时间 = %d\n&quot;</span>, item.pid, item.need_time);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Scheduling::create_node</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> j = <span class="number">0</span>; j &lt; Q_NUM; j++)</span><br><span class="line">    &#123;</span><br><span class="line">        Node node;</span><br><span class="line">        node.priority = <span class="number">1</span> + j;     <span class="comment">// 初始队列最高优先级1</span></span><br><span class="line">        node.cap = <span class="number">20</span> * <span class="built_in">pow</span>(<span class="number">2</span>, j); <span class="comment">// 初始化时间片 20</span></span><br><span class="line">        <span class="keyword">int</span> i;</span><br><span class="line">        <span class="comment">// 在最高优先队列中创建 3 个进程</span></span><br><span class="line">        <span class="keyword">for</span> (i = <span class="number">0</span>; j == <span class="number">0</span> &amp;&amp; i &lt; INIT_PRO_NUM; i++)</span><br><span class="line">        &#123;</span><br><span class="line">            Pro item;</span><br><span class="line">            item.pid = (++q_id);</span><br><span class="line">            item.count = <span class="number">0</span>;</span><br><span class="line">            item.need_time = rand() % <span class="number">100</span> + <span class="number">10</span>; <span class="comment">// 预计需求时间 在 10 ～ 100 之间</span></span><br><span class="line">            item.run_time = <span class="number">0</span>;</span><br><span class="line">            <span class="comment">//</span></span><br><span class="line">            node.q.push(item);</span><br><span class="line">            <span class="built_in">printf</span>(<span class="string">&quot;创建进程 PID= %d:  执行所需时间 = %d\n&quot;</span>, item.pid, item.need_time);</span><br><span class="line">            <span class="built_in">printf</span>(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//</span></span><br><span class="line">        ln.push_back(node);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 先来先服务调度算法</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Scheduling::fcfs</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> i, rd;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;-------先来先服务调度算法-------\n&quot;</span>);</span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; <span class="number">5</span>; i++)</span><br><span class="line">    &#123;</span><br><span class="line">        create_q_pro();</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">while</span> (!qp.empty())</span><br><span class="line">    &#123;</span><br><span class="line">        Pro *p = &amp;qp.front();</span><br><span class="line">        p-&gt;start_time = stime;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;创建进程 PID= %d:  执行所需时间 = %d\n&quot;</span>, p-&gt;pid, p-&gt;need_time);</span><br><span class="line">        sleep(p-&gt;end_time);</span><br><span class="line">        p-&gt;end_time = p-&gt;start_time + stime;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;结束时间%d\n&quot;</span>, p-&gt;end_time);</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">        qp.pop();</span><br><span class="line">        stime = p-&gt;end_time;</span><br><span class="line">        <span class="comment">//</span></span><br><span class="line">        <span class="comment">// rd = rand() % 10;</span></span><br><span class="line">        <span class="comment">// if (rd &gt; 6)</span></span><br><span class="line">        <span class="comment">// &#123;</span></span><br><span class="line">        <span class="comment">//     create_q_pro();</span></span><br><span class="line">        <span class="comment">//     printf(&quot;\n&quot;);</span></span><br><span class="line">        <span class="comment">// &#125;</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 轮转调度算法</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Scheduling::rr</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> i, rd;</span><br><span class="line">    stime = <span class="number">0</span>;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;-------时间片轮转法(时间片 = %d)-------\n&quot;</span>, cap);</span><br><span class="line">    <span class="keyword">for</span> (i = <span class="number">0</span>; i &lt; <span class="number">5</span>; i++)</span><br><span class="line">    &#123;</span><br><span class="line">        create_q_pro();</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">while</span> (!qp.empty())</span><br><span class="line">    &#123;</span><br><span class="line">        Pro *p = &amp;qp.front();</span><br><span class="line">        p-&gt;start_time = stime;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;进程PID=%d: 执行还需时间%d 开始执行时间%d &quot;</span>, p-&gt;pid, p-&gt;need_time, p-&gt;start_time);</span><br><span class="line">        <span class="keyword">if</span> (p-&gt;need_time &gt; cap)</span><br><span class="line">        &#123;</span><br><span class="line">            sleep(cap);</span><br><span class="line">            <span class="comment">// sleep(3);</span></span><br><span class="line">            p-&gt;need_time = p-&gt;need_time - cap;</span><br><span class="line">            p-&gt;end_time = cap;</span><br><span class="line">            p-&gt;run_time = p-&gt;run_time + cap;</span><br><span class="line">            stime = stime + cap;</span><br><span class="line">            ++(p-&gt;count);</span><br><span class="line">            <span class="built_in">printf</span>(<span class="string">&quot;第 %d 次执行，已执行时间 = %d\n&quot;</span>, p-&gt;count, p-&gt;run_time);</span><br><span class="line">            qp.push(qp.front());</span><br><span class="line">            qp.pop();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">else</span></span><br><span class="line">        &#123;</span><br><span class="line">            sleep(p-&gt;end_time);</span><br><span class="line">            <span class="comment">// sleep(3);</span></span><br><span class="line">            stime = stime + p-&gt;end_time;</span><br><span class="line">            p-&gt;end_time = stime;</span><br><span class="line">            p-&gt;run_time = p-&gt;run_time + p-&gt;need_time;</span><br><span class="line">            ++(p-&gt;count);</span><br><span class="line">            <span class="built_in">printf</span>(<span class="string">&quot;第 %d 次执行，已执行时间 = %d 结束时间 = %d 执行完毕\n&quot;</span>, p-&gt;count, p-&gt;run_time, p-&gt;end_time);</span><br><span class="line">            p-&gt;end_time = <span class="number">0</span>;</span><br><span class="line">            qp.pop();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">        <span class="comment">// rd = rand() % 10;</span></span><br><span class="line">        <span class="comment">// if (rd &gt; 6)</span></span><br><span class="line">        <span class="comment">// &#123;</span></span><br><span class="line">        <span class="comment">//     create_q_pro();</span></span><br><span class="line">        <span class="comment">//     printf(&quot;\n&quot;);</span></span><br><span class="line">        <span class="comment">// &#125;</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 多级反馈队列调度算法</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Scheduling::mfq</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> rd;  <span class="comment">// 随机数，随机产生新进程</span></span><br><span class="line">    <span class="keyword">int</span> flag = <span class="number">0</span>; <span class="comment">// flag标志是否有新进程进入初级队列</span></span><br><span class="line">    stime = <span class="number">0</span>; <span class="comment">// 系统的时间</span></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;-------多级反馈队列调度(时间片 = %d)-------\n\n&quot;</span>, cap);</span><br><span class="line">    create_node();</span><br><span class="line">    <span class="comment">// 遍历每个就绪队列</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="built_in">list</span>&lt;Node&gt;::iterator iter = ln.begin(); iter != ln.end();)</span><br><span class="line">    &#123;</span><br><span class="line">        <span class="built_in">printf</span>(<span class="string">&quot;队列优先级 = %d 队列时间片 = %d\n&quot;</span>, iter-&gt;priority, iter-&gt;cap);</span><br><span class="line">        <span class="comment">// 遍历每个就绪队列的每个进程</span></span><br><span class="line">        <span class="keyword">while</span> (!iter-&gt;q.empty())</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="comment">// iter 指向当前队列; iter1 指向下一队列</span></span><br><span class="line">            <span class="built_in">list</span>&lt;Node&gt;::iterator iter1 = iter;</span><br><span class="line">            Pro *p = &amp;iter-&gt;q.front();</span><br><span class="line">            p-&gt;start_time = stime;</span><br><span class="line">            <span class="built_in">printf</span>(<span class="string">&quot;进程PID=%d: 执行还需时间%d 开始执行时间%d &quot;</span>, p-&gt;pid, p-&gt;need_time, p-&gt;start_time);</span><br><span class="line">            <span class="comment">// 进程无法在此队列一次完成</span></span><br><span class="line">            <span class="keyword">if</span> (p-&gt;need_time &gt; iter-&gt;cap)</span><br><span class="line">            &#123;</span><br><span class="line">                <span class="comment">// sleep(cap);</span></span><br><span class="line">                sleep(<span class="number">1</span>);</span><br><span class="line">                p-&gt;need_time = p-&gt;need_time - cap;</span><br><span class="line">                p-&gt;end_time = cap;</span><br><span class="line">                p-&gt;run_time = p-&gt;run_time + cap;</span><br><span class="line">                stime = stime + cap;</span><br><span class="line">                ++(p-&gt;count);</span><br><span class="line">                <span class="built_in">printf</span>(<span class="string">&quot;第 %d 次执行，已执行时间 = %d\n&quot;</span>, p-&gt;count, p-&gt;run_time);</span><br><span class="line">                <span class="keyword">if</span> (++iter1 == ln.end()) <span class="comment">// 如果当前进程正在最后一个队列</span></span><br><span class="line">                &#123;</span><br><span class="line">                    iter-&gt;q.push(iter-&gt;q.front());</span><br><span class="line">                    iter-&gt;q.pop();</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="keyword">else</span> <span class="comment">// 把当前进程放到下一队列队尾</span></span><br><span class="line">                &#123;</span><br><span class="line">                    iter1-&gt;q.push(iter-&gt;q.front());</span><br><span class="line">                &#125;</span><br><span class="line">                <span class="comment">// 从当前队列移除</span></span><br><span class="line">                iter-&gt;q.pop();</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">else</span> <span class="comment">// 进程可以在此队列完成</span></span><br><span class="line">            &#123;</span><br><span class="line">                <span class="comment">// sleep(p-&gt;need_time);</span></span><br><span class="line">                sleep(<span class="number">1</span>);</span><br><span class="line">                stime = stime + p-&gt;need_time;</span><br><span class="line">                p-&gt;end_time = stime;</span><br><span class="line">                p-&gt;run_time = p-&gt;run_time + p-&gt;need_time;</span><br><span class="line">                ++(p-&gt;count);</span><br><span class="line">                <span class="built_in">printf</span>(<span class="string">&quot;第 %d 次执行，已执行时间 = %d 结束时间 = %d 执行完毕\n&quot;</span>, p-&gt;count, p-&gt;run_time, p-&gt;end_time);</span><br><span class="line">                p-&gt;end_time = <span class="number">0</span>;</span><br><span class="line">                iter-&gt;q.pop();</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="built_in">printf</span>(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">            rd = rand() % <span class="number">10</span>;</span><br><span class="line">            <span class="keyword">if</span> (rd &gt; <span class="number">7</span> &amp;&amp; pro_num &lt; TOTAL_PRO_NUM) <span class="comment">//有新进程进入高优先级队列</span></span><br><span class="line">            &#123;</span><br><span class="line">                <span class="built_in">list</span>&lt;Node&gt;::iterator iter2 = ln.begin();</span><br><span class="line">                Pro item;</span><br><span class="line">                item.pid = ++q_id;</span><br><span class="line">                item.count = <span class="number">0</span>;</span><br><span class="line">                item.need_time = rand() % <span class="number">200</span> + <span class="number">10</span>; <span class="comment">// 预计需求时间 在 10 ～ 100 之间</span></span><br><span class="line">                item.run_time = <span class="number">0</span>;</span><br><span class="line">                iter2-&gt;q.push(item);</span><br><span class="line">                pro_num++;</span><br><span class="line">                <span class="built_in">printf</span>(<span class="string">&quot;创建进程 PID= %d:  执行所需时间 = %d\n&quot;</span>, item.pid, item.need_time);</span><br><span class="line">                <span class="built_in">printf</span>(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">                <span class="keyword">if</span> (iter2-&gt;priority &lt; iter-&gt;priority) <span class="comment">// 若当前队列优先级不是最高优先级</span></span><br><span class="line">                &#123;</span><br><span class="line">                    flag = <span class="number">1</span>;</span><br><span class="line">                    <span class="keyword">break</span>;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> (flag == <span class="number">1</span>)</span><br><span class="line">        &#123;</span><br><span class="line">            iter = ln.begin();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">else</span></span><br><span class="line">        &#123;</span><br><span class="line">            ++iter;</span><br><span class="line">        &#125;</span><br><span class="line">        flag = <span class="number">0</span>;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">(<span class="keyword">int</span> argc, <span class="keyword">char</span> <span class="keyword">const</span> *argv[])</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Scheduling sc;</span><br><span class="line">    sc.mfq();</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


    </div>

     <!-- 文章结束表示语-->
    <div>
      
        <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div>
    
</div>
      
    </div>

    
    
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>TANGMONK-MEAT
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://tangmonk-meat.gitee.io/blog/2021/03/19/%E5%A4%9A%E7%BA%A7%E5%8F%8D%E9%A6%88%E9%98%9F%E5%88%97%E8%B0%83%E5%BA%A6%E7%AE%97%E6%B3%95/" title="多级反馈队列调度算法">https://tangmonk-meat.gitee.io/blog/2021/03/19/多级反馈队列调度算法/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/zh-CN" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/blog/tags/%E8%BF%9B%E7%A8%8B/" rel="tag"><i class="fa fa-tag"></i> 进程</a>
              <a href="/blog/tags/C/" rel="tag"><i class="fa fa-tag"></i> C++</a>
              <a href="/blog/tags/%E7%AE%97%E6%B3%95/" rel="tag"><i class="fa fa-tag"></i> 算法</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/blog/2021/03/19/thymeleaf%E5%BC%95%E5%85%A5%E5%85%AC%E5%85%B1%E9%83%A8%E5%88%86/" rel="prev" title="thymeleaf引入公共部分">
      <i class="fa fa-chevron-left"></i> thymeleaf引入公共部分
    </a></div>
      <div class="post-nav-item">
    <a href="/blog/2021/08/02/%E6%A0%B9%E6%8D%AE%E9%A1%B9%E7%9B%AE%E4%BD%8D%E7%BD%AE%E8%87%AA%E5%8A%A8%E5%8A%A0%E8%BD%BD%E5%B9%B6%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E8%B7%AF%E5%BE%84/" rel="next" title="根据项目位置自动加载并配置文件路径">
      根据项目位置自动加载并配置文件路径 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



        </div>
        
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">TANGMONK-MEAT</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
      <span class="post-meta-item-text">站点总字数：</span>
    <span title="站点总字数">148k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
      <span class="post-meta-item-text">站点阅读时长 &asymp;</span>
    <span title="站点阅读时长">2:15</span>
</div>

<!--
-->

<!--<div>
<span id="busuanzi_container_site_pv">
    本站总访问量<span id="busuanzi_value_site_pv"></span>次
</span> 
 <span class="post-meta-divider">|</span> 
 <span class="post-meta-item-icon">
<span id="busuanzi_container_site_uv">
  本站访客数<span id="busuanzi_value_site_uv"></span>人次
</span>
</div> 

-->




        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/blog/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/lozad@1/dist/lozad.min.js"></script>

<script src="/blog/js/utils.js"></script>

<script src="/blog/js/motion.js"></script>


<script src="/blog/js/schemes/muse.js"></script>


<script src="/blog/js/next-boot.js"></script>


  
  <script>
    (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      bp.src = (curProtocol === 'https') ? 'https://zz.bdstatic.com/linksubmit/push.js' : 'http://push.zhanzhang.baidu.com/push.js';
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
    })();
  </script>




  
<script src="/blog/js/local-search.js"></script>













  

  


<script>
NexT.utils.loadComments('#valine-comments', () => {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/valine@1/dist/Valine.min.js', () => {
    new Valine(Object.assign({
      el  : '#valine-comments',
      path: location.pathname,
    }, {"enable":true,"appId":"dkMV7sEQR97E6CWI5jduGKf3-gzGzoHsz","appKey":"JQfXJ0ELtfNma5bdMQk0vgwX","placeholder":"ヾﾉ≧∀≦)o来啊，快活啊!","avatar":"mm","meta":["nick","mail"],"pageSize":10,"language":"zh-cn","visitor":true,"comment_count":true,"recordIP":true,"serverURLs":null,"enableQQ":false,"requiredFields":[]}
    ));
  }, window.Valine);
});
</script>

</body>
</html>
